from typing import Any, Text, Dict, List
from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
from .neo4j_connection import graph
from .api_instance import api_instance


class ActionAskAboutEventParticipate(Action):
    def name(self) -> Text:
        return "action_ask_about_event_participate"

    async def run(self, dispatcher: CollectingDispatcher,
                  tracker: Tracker,
                  domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
        event_name = next(tracker.get_latest_entity_values("event"), None)
        event_list = graph.query_event(event_name)

        if not event_name:
            api_instance.ask(tracker.events, dispatcher)
            return []

        if not event_list:
            dispatcher.utter_message("不好意思，我不清楚你问的" + (event_name if event_name else "") + "这件事。", attachment="finish")
            return []

        participant_list = graph.query_event_participate(event_name)

        if not participant_list:
            dispatcher.utter_message("不好意思，这个事件参与者我也说不太清都有什么人。", attachment="finish")
            return []

        message = (event_name + "的参与者有：")

        for record in participant_list:
            message += record["name"] + "，"

        message = message[:-1] + "。"

        api_instance.optimize_response(tracker.events, message, dispatcher)
        return []
